<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<script type="text/javascript" src="${CONTEXT_PATH}/js/login/business/charge/ChargeConstant.js"></script>
<%--该页为嵌入页面--%>
<s:hidden name="tuition.totalAmount" id="totalAmount"></s:hidden>
<div style="width:100%;" id="indexAddDiv">
	<s:iterator value="tirList" status="s">
		<div class="indexDiv" id="indexDiv<s:property value='id' />">
			<nobr>
			<s:select name="indexGroup" id="indexSel%{id}" list="indexList" listKey="id" listValue="indexName" cssStyle="width:120px" value="%{chargeIndex.id}" onchange="checkTotalAmount()" isOnce="${chargeIndex.isOnce}"></s:select>
			￥
			<s:textfield name="amountGroup" id="amountTxt%{id}" cssStyle="width:50px" value="%{intAmount}" onblur="amountCheck('%{id}')"></s:textfield>
			从
			<input type="text" name="dateStartGroup" id="dateStart<s:property value='id' />" style="width:100px" class="WDate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" onblur="dateBlur(this)" isOnce="<s:property value='chargeIndex.isOnce' />" />
			至
			<input type="text" name="dateEndGroup" id="dateEnd<s:property value='id' />" style="width:100px" class="WDate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" onblur="checkTotalAmount()" isOnce="<s:property value='chargeIndex.isOnce' />" />
			&nbsp;<a href="javascript:clearIndex('<s:property value="id" />')">删除</a>
			</nobr>
		</div>
	</s:iterator>
</div>
<br>
<table cellpadding="0" cellspacing="0" width="100%">
	<tr>
		<td width="15%" align="left" height="30px">
			<a href="javascript:addIndex()">添加项</a>
		</td>
		<td width="20%" align="right">
			保教费折扣：
		</td>
		<td width="15%" align="left">
			<s:select name="discount" id="discount" list="discountList" listKey="value" listValue="label" cssStyle="width:100px;" onchange="checkTotalAmount()"></s:select>
		</td>
		<td width="35%" align="right">
			总金额（￥）：
		</td>
		<td width="15%" align="left">
			<div style="display:inline; width:100px;" id="totalAmountDiv" align="left"></div>
		</td>
	</tr>
</table>

<div id="indexDivTemp" style="display:none;">
	<div class="indexDiv" id="indexDiv#tirId#">
		<nobr>
		<s:select name="indexGroup" id="indexSel#tirId#" list="indexList" listKey="id" listValue="indexName" cssStyle="width:120px" onchange="checkTotalAmount()"></s:select>
		￥
		<s:textfield name="amountGroup" id="amountTxt#tirId#" cssStyle="width:50px" onblur="amountCheck('#tirId#')"></s:textfield>
		从
		<input type="text" name="dateStartGroup" id="dateStart#tirId#" style="width:100px" class="WDate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" onblur="checkTotalAmount()" />
		至
		<input type="text" name="dateEndGroup" id="dateEnd#tirId#" style="width:100px" class="WDate" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" onblur="checkTotalAmount()" />
		&nbsp;<a href="javascript:clearIndex('#tirId#')">删除</a>
		</nobr>
	</div>
 </div>

<script>
	var n = 0;
    var handId = 'hand_';
    var firstNotOnce = null;

    function tempInit()
    {
    	initIndexOrder();
    	initOnceDate();
    	checkTotalAmount();
    }
    
    function amountCheck(id)
    {
    	var amountTxtx = $('#amountTxt' + id);
    	if(isNotNull(amountTxtx.val()) && checkFloat(amountTxtx) == false)
    	{
    		showMsg('金额有误。', function(){amountTxtx.focus();});
    		amountTxtx.val('');
    		return;
    	}
    	checkTotalAmount();
    }
    
    function clearIndex(id)
    {
    	var indexDivx = $('#indexDiv' + id);
    	indexDivx.remove();
    	checkTotalAmount();
    }
    
    function addIndex()
    {
    	var indexDivTemp = $('#indexDivTemp');
    	var html = indexDivTemp.html();
    	var newId = handId + n;
    	html = html.replaceAll('#tirId#', newId);
    	var indexAddDiv = $('#indexAddDiv');
    	indexAddDiv.html(indexAddDiv.html() + html);
    	n++;
    }
    
    function computeTotalAmount()
    {
    	var indexGroup = $('select[name="indexGroup"]');
    	var amountGroup = $('input:text[name="amountGroup"]');
    	var dateStartGroup = $('input:text[name="dateStartGroup"]');
    	var dateEndGroup = $('input:text[name="dateEndGroup"]');
    	var discount = $('#discount');
    	if(!checkInteger(discount) && !checkFloat(discount))
    	{
   			showMsg('折扣有误，不为浮点型。', function(){discount.focus();});
    		return 0;
    	}
    	var len = indexGroup.length;
    	var sum = 0.00;
    	for(var i = 0; i < len - 1; i++)
    	{
    		var amountx = $(amountGroup.get(i));
    		if(!isNotNull(amountx.val()))
    		{
    			return 0;
    		}
    		var dateStartx = $(dateStartGroup.get(i));
    		var dateEndx = $(dateEndGroup.get(i));
    		if(!isNotNull(dateStartx.val()))
    		{
    			return 0;
    		}
    		if(!isNotNull(dateEndx.val()))
    		{
    			return 0;
    		}
    		var yearStartx = dateStartx.val().split('-')[0];
    		var yearEndx = dateEndx.val().split('-')[0];
    		var monthStartx = dateStartx.val().split('-')[1];
    		var monthEndx = dateEndx.val().split('-')[1];
    		var sMonth = parseInt(yearStartx) * 12 + parseInt(monthStartx);
    		var eMonth = parseInt(yearEndx) * 12 + parseInt(monthEndx);
    		if(dateStartx.val() > dateEndx.val())
    		{
    			showMsg('开始日期不能大于结束日期。');
    			return 0;
    		}
    		if((yearStartx + monthStartx) != (yearEndx + monthEndx))
    		{
    			showMsg('同一指标开始日期与结束日期年月必须相同。');
    			return 0;
    		}
    		var indexAmount = parseFloat(amountx.val()) * (eMonth - sMonth + 1);
    		
    		if(SCHOOLING == $(indexGroup.get(i)).find('option:selected').text())
    		{
    			indexAmount = indexAmount * parseFloat(discount.val());
    		}
    		sum += toNBitFloat(indexAmount, 0);
    	}
    	sum = toNBitFloat(sum, 0);
    	return sum;
    }
    
    function checkTotalAmount()
    {
    	var totalAmount = $('#totalAmount');
    	var totalAmountDiv = $('#totalAmountDiv');
    	var sum = computeTotalAmount();
    	totalAmount.val(sum);
    	totalAmountDiv.text(sum);
    }

    function initIndexOrder()
    {
    	var indexGroup = $('select[name="indexGroup"]');
    	var dateStartGroup = $('input:text[name="dateStartGroup"]');
    	var dateEndGroup = $('input:text[name="dateEndGroup"]');
    	var len = indexGroup.length;
    	var orderObj = {};
    	var y = 100;
    	for(var i = 0; i < len - 1; i++)
    	{
    		var indx = $(indexGroup.get(i));
    		var dateStartx = $(dateStartGroup.get(i));
    		var dateEndx = $(dateEndGroup.get(i));
    		if('0' == indx.attr('isOnce'))
    		{
	        	if(firstNotOnce == null)
	        	{
	        		firstNotOnce = dateStartx;
	            }
	
	            var x = orderObj[indx.val()];
	            if(x == null)
	            {
	            	orderObj[indx.val()] = 0;
	            }
	            else
	            {
	            	orderObj[indx.val()] = x + 1;
	            }
	            indx.attr('row', orderObj[indx.val()]);
	            dateStartx.attr('row', orderObj[indx.val()]);
	            dateEndx.attr('row', orderObj[indx.val()]);
    		}
    		else
    		{
    			dateStartx.attr('row', y); //从100开始
	            dateEndx.attr('row', y);
	            y++;
    		}
    	}
    }

    function initOnceDate()
    {
		var dateStartGroupOnce = $('input:text[name="dateStartGroup"][isOnce="1"]');
		var len = dateStartGroupOnce.length;
		for(var i = 0; i < len; i++)
		{
			var dateStartx = $(dateStartGroupOnce.get(i));
			var rowx = dateStartx.attr('row');
			var dateEndGroupOnce = $('input:text[name="dateEndGroup"][row="' + rowx + '"]');
			if(dateEndGroupOnce.length > 0)
			{
				var dateEndx = $(dateEndGroupOnce.get(0));
				var today = DateUtil.Format('yyyy-MM-dd', new Date());
				dateStartx.val(today);
				dateEndx.val(today);
			}
		}
    }

    function autoDate(jqObj)
    {
    	var indexGroup = $('select[name="indexGroup"][isOnce="0"]');
    	var dateStartGroup = $('input:text[name="dateStartGroup"][isOnce="0"]');
    	var dateEndGroup = $('input:text[name="dateEndGroup"][isOnce="0"]');
    	var len = indexGroup.length;
    	var firstDate = jqObj.val();
    	for(var i = 0; i < len; i++)
    	{
        	if(!isNotNull(firstDate))
				return;
    		var indx = $(indexGroup.get(i));
    		var dateStartx = $(dateStartGroup.get(i));
    		var dateEndx = $(dateEndGroup.get(i));
    		var dates = firstDate.split('-');
    		var year = parseInt(dates[0], 10);
    		var month = parseInt(dates[1], 10);
    		var day = dates[2];
    		var row = parseInt(dateStartx.attr('row'), 10);
    		var newDate = year * 12 + month + row;
    		var newYear = parseInt(newDate / 12, 10);
    		var newMonth = parseInt(newDate % 12, 10);
    		if(newMonth == 0)
    		{
    			newMonth = 12;
    			newYear -= 1;
        	}
    		if(newMonth < 10)
    		{
    			newMonth = '0' + newMonth;
    		}
			var lastEndDate = new Date(newYear, newMonth, 0);
			var last = lastEndDate.getDate();
			if(parseInt(last, 10) < parseInt(day, 10))
			{
				day = last + '';
			}
			var newDateStr = newYear + '-' + newMonth + '-' + day;

			var lastEndDateStr = newYear + '-' + newMonth + '-' + last;
    		dateStartx.val(newDateStr);
    		dateEndx.val(lastEndDateStr);
    	}
    }

    function dateBlur(obj)
    {
        var thisObj = $(obj);
        if(thisObj.attr('id') == firstNotOnce.attr('id'))
        {
        	autoDate(thisObj);
        }
    	checkTotalAmount();
    }
</script>